package com.atguigu.guli.service.vod.controller.api;

import com.aliyuncs.exceptions.ClientException;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.vod.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lsj
 * @create 2021-07-13 18:22
 */
@Api(tags = "阿里云视频点播")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/api/vod/media")
@Slf4j
public class ApiMediaController {

    @Autowired
    private VideoService videoService;

    @GetMapping("get-play-auth/{videoSourceId}")
    public R getPlayAuth(
            @ApiParam(value = "阿里云视频文件的id", required = true)
            @PathVariable String videoSourceId){

        try{
            String playAuth = videoService.getPlayAuth(videoSourceId);
            return  R.ok().message("获取播放凭证成功").data("playAuth", playAuth);
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR);
        }
    }

    /**
     * 方式2：获取视频地址
     * @param videoSourceId
     * @return
     */
    @GetMapping("get-play-info/{videoSourceId}")
    public R getPlayInfo(
            @ApiParam(value = "阿里云视频id", required = true)
            @PathVariable String videoSourceId){
        try {
            String playInfo = videoService.getPlayInfo(videoSourceId);
            return R.ok().data("playInfo", playInfo);
        } catch (ClientException e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYINFO_ERROR);
        }
    }
}